package com.hl.im.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServerRequest;

import java.util.UUID;

/**
 * @author huanglin
 * @apiNote
 * @date: 2023/10/12 18:22
 */
@Component
public class TraceGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        request.mutate().headers(headers -> {
            headers.add("Trace_Id", UUID.randomUUID().toString());
        });
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -100;
    }
}
